var tmx=null;
var tmy=null;

var lockReconnect = false;
var tt;
let returnData;
let returnData2;
let returnData3;
var flag = true;
//var path = '192.168.129.157:8081/hmmp/';
//var path = '192.168.129.161:8081/hmmp/';
var path = '192.168.50.53:83/';
// "signCode_monitor_type_longtime"
var math = Math.floor(Math.random()*10);
var userCode = "lzzzz"; // 随机数
var websocket;

function createWebSocket(){
    try{
        if ('WebSocket' in window) {
            websocket = new WebSocket("ws://" + path + "wsz?userId=" + userCode+"&&bizType=2");
            init();
        } else if ('MozWebSocket' in window) {
            websocket = new MozWebSocket("ws://" + path + "wsz?userId=" + userCode+"&&bizType=2");
        } else {
            websocket = new SockJS("http://" + path + "wsz/sockjs?userId=" + userCode+"&&bizType=2");
        }
    } catch(e){
        console.log(e);
        reconnect();
    }
}

function reconnect() {
    if(lockReconnect) {
        return;
    };
    lockReconnect = true;
    //没连接上会一直重连，设置延迟避免请求过多
    tt && clearTimeout(tt);
    tt = setTimeout(function () {
        createWebSocket();
        lockReconnect = false;
    }, 4000);
}

function init(){
    websocket.onopen = function(event) {
        console.log("WebSocket:已连接");
        console.log(event);
    };
    websocket.onmessage = function(event) {
        var data = JSON.parse(event.data);
        console.log(new Date()+"当前用户"+userCode+"WebSocket:收到一条消息-form", data);
    };
    websocket.onerror = function(event) {
        console.log("WebSocket:发生错误 ");
        console.log(event);
        reconnect();
    };
    websocket.onclose = function(event) {
        console.log("WebSocket:已关闭");
        console.log(event);
        reconnect();
    }
}

createWebSocket();